package com.ulife.category.manager.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.ulife.category.dao.PropertyDao;
import com.ulife.category.dao.PropertyValueDao;
import com.ulife.category.dao.dataobject.PropertyValueDo;
import com.ulife.category.manager.PropertyManager;
import com.ulife.category.module.action.PropertyAction;
import com.ulife.category.module.vo.PropertyVo;

public class PropertyManagerImpl implements PropertyManager {

	private static final Logger log = LoggerFactory.getLogger(PropertyAction.class);

	@Autowired
	private PropertyDao propertyDao;

	@Autowired
	private PropertyValueDao propertyValueDao;

	public int addPropertyManager(PropertyVo propertyVo) {
		propertyVo.setpStatus(1);
		try {
			Long pid = propertyDao.insertProperty(propertyVo);
			if (pid == null || pid == 0) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return 0;
			}
			propertyVo.setpId(pid);

			String pv = propertyVo.getpValues();
			String[] pvArray = pv.split(";");
			String[] pvone = null;
			for (int i = 0; i < pvArray.length; i++) {
				PropertyValueDo pvdo = new PropertyValueDo();
				pvdo.setpId(propertyVo.getpId());
				if (StringUtils.isNotBlank(pvArray[i])) {
					pvone = StringUtils.split(pvArray[i], ":");
					pvdo.setPvName(pvone[0]);
					if (pvone.length > 1) {
						pvdo.setPvCode(pvone[1]);
					}

					Long pvid = propertyValueDao.insertPropertyValue(pvdo);
					if (pvid == null || pvid == 0) {
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						return 0;
					}
				}
			}
		} catch (Exception e) {
			log.error("addPropertyManager ", e);
			// 貌似一定要显示回滚
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return 0;
		}
		return 1;
	}

	public int delPropertyManager(PropertyVo propertyVo) {
		Long pId = propertyVo.getpId();
		if (pId == null || pId <= 0) {
			return 0;
		}
		try {
			propertyValueDao.deletePropertyValueByPid(pId);
			int res1 = propertyDao.deleteProperty(propertyVo);
			if (res1 <= 0) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return 0;
			}
		} catch (Exception e) {
			log.error("delPropertyManager pId=" + pId, e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return 0;
		}
		return 1;
	}

	public static void main(String[] args) {
		String[] arr = StringUtils.split("1;2;;;", ";");
		System.out.println(arr.length);
	}

	public int updatePropertyManager(PropertyVo propertyVo) {
		Long pId = propertyVo.getpId();
		if (pId == null || pId <= 0) {
			return 0;
		}
		String newPvStr = propertyVo.getpValues();
		String[] newPvArray = StringUtils.split(newPvStr, ";");
		String[] oldPvInNewArray = {};
		PropertyValueDo[] newPvInNewArray = {};
		String[] pvonearry = {};
		for (String pvOne : newPvArray) {
			pvonearry = pvOne.split(":");
			if (!StringUtils.equals(pvonearry[0], "0")) {
				oldPvInNewArray = (String[]) ArrayUtils.add(oldPvInNewArray, pvonearry[0]);
			} else {
				PropertyValueDo pv = new PropertyValueDo();
				pv.setpId(pId);
				pv.setPvName(pvonearry[1]);
				if (pvonearry.length == 3) {
					pv.setPvCode(pvonearry[2]);
				}
				newPvInNewArray = (PropertyValueDo[]) ArrayUtils.add(newPvInNewArray, pv);
			}
		}

		try {
			List<PropertyValueDo> pvDos = propertyValueDao.queryPropertyValuesByPId(pId);
			List<Long> pvidssfordelete = new ArrayList<Long>();
			for (PropertyValueDo propertyValueDo : pvDos) {
				if (ArrayUtils.indexOf(oldPvInNewArray, propertyValueDo.getPvId() + "") == ArrayUtils.INDEX_NOT_FOUND) {
					pvidssfordelete.add(propertyValueDo.getPvId());
				}
			}
			if(pvidssfordelete.size()>0){
				propertyValueDao.deletePropertyValueByIds(pvidssfordelete);
			}

			for (int i = 0; i < newPvInNewArray.length; i++) {
				propertyValueDao.insertPropertyValue(newPvInNewArray[i]);
			}

			propertyDao.updateProperty(propertyVo);
		} catch (Exception e) {
			log.error("updatePropertyManager pId=" + pId, e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}

		return 1;
	}
}
